/// @author: cairuoyu
/// @homepage: http://cairuoyu.com
/// @github: https://github.com/cairuoyu/cry、https://github.com/cairuoyu/flutter_admin
/// @date: 2021/6/21
/// @version: 1.0
/// @description:

import 'package:flutter_admin/pages/common/cry/common/application_context.dart';
import 'package:flutter_admin/pages/common/cry/constants/cry_constant.dart';
import 'package:flutter_admin/pages/common/cry/model/application.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter_admin/constants/constant.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:odoo_rpc/odoo_rpc.dart';

class OdooHttpUtil {

  static OdooClient? client;

  static Future<ResponseBodyApi> callRPC(String url, {data}) async {
    data = data ?? {};
    ResponseBodyApi responseBodyApi;
    try {
      await getColient();
      var res = await client!.callRPC(url, 'call', data);
      responseBodyApi = ResponseBodyApi(success: true, data: res);
    } catch (e) {
      print(e);
      responseBodyApi = ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<dynamic> callKw({model='',method='',args,kwargs,fieldsInfo,filter,bool flag = true}) async {
    args = args ?? [];
    kwargs = kwargs ?? {};
    fieldsInfo = fieldsInfo ?? {};
    filter = filter ?? {};
    print("============================");
    print(''''model': $model,
    'method': $method,
    'args': $args,
    'kwargs': $kwargs,
    'fields_info': $fieldsInfo,
    'filter': $filter,''');
    dynamic data;
    try {
      await getColient();
      data = await client!.callKw({
        'model': model,
        'method': method,
        'args': args,
        'kwargs': kwargs,
        'fields_info': fieldsInfo,
        'filter': filter,
      });
    } on OdooSessionExpiredException catch (e){
      if(flag){
        client = null;
        await getColient();
        data = await callKw(model: model,method: method,args: args,kwargs: kwargs,fieldsInfo: fieldsInfo,filter: filter,flag: false);
      }else{
        print(e);
        CryUtils.message('请求出错了：'+e.message);
        throw e;
      }
    } on OdooException catch (e) {
      print(e);
      CryUtils.message('请求出错了：'+e.message);
      throw e;
    }

    return data;
  }

  static Future<dynamic> getColient() async{
    dynamic session;
    if (client == null) {
      Application application = ApplicationContext.instance.application;
      var odooBaseUrl =application.odooBaseUrl!;
      var database =application.database!;
      client = OdooClient(odooBaseUrl);
      try {
        var data = StoreUtil.read(Constant.CLIENT);
        session = await client!.authenticate(database, data["login"], data["password"]);
        print(session);
      } catch (e) {
        client = null;
        print(e);
        throw e;
      }
    }

    return session;
  }

  static Future<dynamic> createInstance(data) async {
    dynamic session;
    try {
      client = null;
      StoreUtil.write(Constant.CLIENT, data);
      session = await getColient();
      print(session);
    } catch (e) {
      print(e);
      StoreUtil.cleanAll();
      throw e;
    }
    return session;
  }

  static clear() {
    if(client != null){
      client!.close();
    }
    client = null;
  }

}
